package com.personnel.service.impl;

import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.net.URLEncoder;

/**
 * @Author: YuePeng
 * @Date: 2022/2/16 16:46
 * @Version: 1.0
 */
@Service
public class DownloadExcelTemplateServiceImpl {
    public void downloadGovernmentExcelTemplate(HttpServletRequest request,HttpServletResponse response) throws IOException {
        //文件地址
        String fileName = "D:\\IdeaProject\\personnel_management_system\\src\\main\\resources\\static\\downloadTemplates\\新增员工信息填写模板.xls";
        //String fileName = "downloadTemplates/新增员工信息填写模板.xls";
        //获取输入流
        InputStream bis = new BufferedInputStream(new FileInputStream(fileName));
        fileName = URLEncoder.encode("新增员工信息填写模板.xls","UTF-8");
        //设置文件下载头
        response.addHeader("Content-Disposition", "attachment;filename=" + fileName);
        //设置文件ContentType类型，这样设置，会自动判断下载文件类型
        response.setContentType("multipart/form-data");
        BufferedOutputStream out = new BufferedOutputStream(response.getOutputStream());
        int len = 0;
        while((len = bis.read()) != -1){
            out.write(len);
            out.flush();
        }
        out.close();
    }
}
